//
//  NewShopcartModel.m
//  FD
//
//  Created by xiaofang wang on 2018/11/20.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "NewShopcartModel.h"
//加料字体
#define kJiaLiaoFontSize 15

@implementation NewShopcartModel
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"cartId":@"id"};
}

//计算cell高度
- (CGFloat)cellHeight {
    //如果计算过了就直接返回
    if (_cellHeight) {
        return _cellHeight;
    }
    
    //第一部分:间距 + 标题 + 第二部分:价格 加减按钮
    _cellHeight = 10 + 20 + 30;

    //第三部分:配料 动态计算高度。换行
    NSString *jiaLiaoStr = [self jiaLiaoStrWithingredientClassifyArr:self.goods[@"ingredientClassify"]];
    
    CGFloat strHeight = 0.0;
    //@""
    
    if (![NSString isEmptyString:jiaLiaoStr]) { //非空 nil/@""
        //屏幕宽 - 30
        strHeight = [self heightForString:jiaLiaoStr font:[UIFont systemFontOfSize:kJiaLiaoFontSize] width:SCREEN_WIDTH - 30];
    }
    _cellHeight += strHeight;
    
    FDLog(@"文字高度:%f",strHeight);
    //第四部分:备注
    _cellHeight += 20;
    
    _cellHeight += 10; //间距
    
    FDLog(@"cellHeight:%f",_cellHeight);
    //返回
    return _cellHeight;
}




#pragma mark - 计算总的原价 & 总的现价
- (CGFloat)totalOriginPrice {
    
    //原价总和
    __block CGFloat ySum = [self.goods[@"originalPrice"] floatValue];
    
    //便利数组计算总价
    NSArray *ingredientClassify = self.goods[@"ingredientClassify"];
    if (ingredientClassify.count < 1) {
        return ySum;
    }
    
    for (NSDictionary *dic1 in ingredientClassify) {
        NSArray *ingredientList = dic1[@"ingredientList"];
        //表示前 n 显示是免费的,不参与价格计算
       NSInteger free = [dic1[@"free"] integerValue];
        if (free < ingredientList.count) {
            for (NSInteger i=free; i<ingredientList.count; i++) {
                NSDictionary *secondLevelDic = ingredientList[i];
                CGFloat op = [secondLevelDic[@"originalPrice"] floatValue];
                FDLog(@"op要钱的:%f",op);
                ySum += op;
            }
        }
        FDLog(@"========");
    }
    return ySum;
}

- (CGFloat)totalcurrentPrice {
    //现价总和
    __block CGFloat xSum = [self.goods[@"currentPrice"] floatValue];
    
    //数组
    for (NSDictionary *firstLevelDic in self.goods[@"ingredientClassify"]) {
        NSInteger free = [firstLevelDic[@"free"] integerValue];
        NSArray *ingredientListArr = firstLevelDic[@"ingredientList"];
        
        
        //前几个不要钱 从 free 开始。
        //如果free>数组个数 ==>都不要钱
        //如果free<数组个数 ==>前几个不要钱,要钱的从free开始算
        if (free < ingredientListArr.count) {
            for (NSInteger i=free; i<ingredientListArr.count; i++) {
                NSDictionary *secondLevelDic = ingredientListArr[i];
                CGFloat cp = [secondLevelDic[@"currentPrice"] floatValue];
                FDLog(@"cp要钱的:%f",cp);
                //前几个不要前怎么判断
                xSum += cp;
            }
        }
        FDLog(@"=========\n");
    }
    
    return xSum;
}

//拼接加料文字
- (NSString *)jiaLiaoStrWithingredientClassifyArr:(NSArray *)arr {
    FDLog(@"%@",arr);
    
    NSMutableArray *firstArrM = [NSMutableArray array];
    
    for (NSDictionary *firstLevelDic in arr) {
        NSString *name = firstLevelDic[@"name"];
        NSArray *ingredientListArr = firstLevelDic[@"ingredientList"];
        
        NSMutableArray *secondTempArrM = [NSMutableArray array];

        for (NSDictionary *secondLevelDic in ingredientListArr) {
            NSString *secondName = secondLevelDic[@"name"];
            [secondTempArrM addObject:secondName];
        }
        //拼接 、
        NSString *secondStr = [secondTempArrM componentsJoinedByString:@"、"];
        
        //测试分类1:xxx1、xxx2
        secondStr = [name stringByAppendingFormat:@":%@",secondStr];
        [firstArrM addObject:secondStr];
    }
    
    //拼接换行符号 "\n"
    NSString *result = [firstArrM componentsJoinedByString:@"\n"];
    return result;
}


#pragma mark - 1-10计算换行文字高度
- (CGFloat )heightForString:(NSString *)str font:(UIFont *)font width:(CGFloat)width{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
    NSDictionary * attributes = @{
                                  NSFontAttributeName:font,
                                  NSParagraphStyleAttributeName: paragraphStyle
                                  };
    //    CGSize textRect = CGSizeMake([UIScreen mainScreen].bounds.size.width - 30, MAXFLOAT);
    CGSize textRect = CGSizeMake(width, MAXFLOAT);
    
    CGFloat textHeight = [str boundingRectWithSize: textRect
                                           options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                        attributes:attributes
                                           context:nil].size.height;
    return textHeight;
}

@end
